<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height: 60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="showDialog=true"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table border="" :data="list">
              <el-table-column align="center" type="index" label="序号" width="120" />
              <el-table-column align="center" prop="name" label="角色名称" width="240" />
              <el-table-column align="center" prop="description" label="描述" />

              <el-table-column align="center" label="操作">
                <template v-slot="{row}">
                  <el-button size="small" type="success" @click="showPermission(row.id)">分配权限</el-button>
                  <el-button size="small" type="primary" @click="onCompile(row.id)">编辑</el-button>
                  <el-button size="small" type="danger" @click="onDelete(row.id)">删除</el-button>

                </template>
              </el-table-column>

            </el-table>
            <!-- 放置分页组件 -->
            <el-row type="flex" justify="center" align="middle" style="height:60px">
              <el-pagination
                :current-page="page.page"
                :page-size="page.pagesize"
                :total="page.total"
                layout="prev, pager, next"
                @current-change="changePage"
              />
            </el-row>
            <!-- 弹框 -->
            <el-dialog :title="roleForm.id?'编辑信息':'新增信息'" :visible="showDialog" @close="btnCancel">
              <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="120px">
                <el-form-item label="角色名称" prop="name">
                  <el-input v-model="roleForm.name" />
                </el-form-item>
                <el-form-item label="角色描述" prop="description">
                  <el-input v-model="roleForm.description" />
                </el-form-item>
              </el-form>
              <!-- 底部 -->
              <el-row slot="footer" type="flex" justify="center">
                <el-col :span="6">
                  <el-button size="small" @click="btnCancel">取消</el-button>
                  <el-button size="small" type="primary" @click="btnOK">确定</el-button>
                </el-col>
              </el-row>
            </el-dialog>

          </el-tab-pane>
          <!-- 表单 -->
          <el-tab-pane label="公司信息">
            <el-form label-width="120px" style="margin-top:50px">
              <el-form-item label="公司名称">
                <el-input v-model="formData.name" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input v-model="formData.companyAddress" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input v-model="formData.mailbox" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="备注">
                <el-input v-model="formData.remarks" type="textarea" :rows="3" disabled style="width:400px" />
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </el-card>
      <!-- 分配弹框 begin -->
      <el-dialog title="分配权限" :visible="isShowPermission" @close="btnPermCancel">
        <!-- data 是数据数组, 套着一堆对象 props 指定要显示的字段,
        default-expand-all 默认展开所有 -->
        <!-- 本来多个多选框需要自己 v-for 遍历, 但是这里由树形处理了 -->
        <el-checkbox-group v-model="checkList">
          <!-- 本来多个多选框需要自己 v-for 遍历, 但是这里由树形处理了 -->

          <!-- 每个属性节点都是一个多选框 -->
          <!-- <el-checkbox v-for="item in permList" :key="item.id" :label="item.id"> -->
          <!-- {{ item.name }} -->
          <!-- </el-checkbox> -->
          <el-tree :data="permList" default-expand-all>
            <template v-slot="scope">
              <el-checkbox :label="scope.data.id"> {{ scope.data.name }}</el-checkbox>
            </template>
          </el-tree>

        </el-checkbox-group>
        <template #footer>
          <el-button size="small" type="primary" @click="btnPermOK">确定</el-button>
          <el-button size="small" @click="btnPermCancel">取消</el-button>
        </template>
      </el-dialog>
      <!-- 分配弹框 end -->

    </div>
  </div>
</template>

<script>
import { getCompanyInfo, getRoleList, deleteRole, getRoleDetail, updateRole, addRole, assignPerm } from '@/api/setting'
import { Message } from 'element-ui'
import { mapGetters } from 'vuex'
import { getPermissionList } from '@/api/permisson'
import { listToTree } from '@/utils'
export default {

  data() {
    return {
      showDialog: false,
      roleForm: {},
      formData: {},
      list: [], // 承接数组
      page: {
        page: 1, // 页码
        pagesize: 10, // 每页条数
        total: 0 // 总页数
      },
      rules: {
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }],
        description: [{ required: true, message: '角色描述不能为空', trigger: 'blur' }]
      },
      isShowPermission: false,
      // 被选中的权限数组
      checkList: [],
      // 所有权限数组
      permList: [],
      // 点击弹框时存储角色id方便复用
      roleId: ''
    }
  },
  computed: {
    ...mapGetters(['companyId'])
  },
  async  created() {
    // 获取公司信息
    this.formData = await getCompanyInfo(this.companyId)
    // 在页面加载的时候调用 角色列表
    this.getRoleList()

    // 获取权限列表
    const res = await getPermissionList()
    // console.log(res)
    // 权限数据是树形需要递归转换
    // this.permList = res
    this.permList = listToTree(res, '0')
  },
  methods: {
    async getRoleList() {
      // 获取角色列表
      const { rows, total } = await getRoleList(this.page, this.pagesize)
      this.page.total = total// 获取到的总页数
      this.list = rows// 把获取到的值给空数组
      // console.log(rows)
    },
    changePage(newPage) {
      this.page.page = newPage // 将当前页码赋值给当前的最新页码
      // 渲染到页面上
      this.getRoleList()
    },
    // 删除角色
    onDelete(id) {
      try {
        this.$confirm('此操作将永久删除, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async() => {
          const res = await deleteRole(id)
          this.getRoleList()
          console.log(res)
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
      } catch (error) {
        console.log(error)
      }
    },
    // 取消
    btnCancel() {
      // 模态框消失
      this.showDialog = false
      this.roleForm = {
        name: '',
        description: ''
      }
      this.$refs.roleForm.resetFields()
    },
    // 确认修改数据
    async btnOK() {
      try {
        // 校验表单
        await this.$refs.roleForm.validate()
        // 判断是新增还是编辑
        if (this.roleForm.id) {
        // 是编辑发出请求
          await updateRole(this.roleForm)
          // console.log(this.roleForm)
          this.$message('更新角色详情成功')
        } else {
          await addRole(this.roleForm)
          Message.success('新增角色成功')
          console.log(this.roleForm)
        }
        // 弹出警示框
        // this.$message('成功')
        // 关闭模态框
        this.showDialog = false
        // 渲染到页面上
        this.getRoleList()
      } catch (error) {
        console.log(error)
      }
    },
    // 编辑获取到数据
    async onCompile(id) {
      this.showDialog = true
      this.roleForm = await getRoleDetail(id)
    },
    // 确定 修改权限
    async   btnPermOK() {
      // 调用接口
      await assignPerm({
        id: this.roleId,
        permIds: this.checkList
      })
      // 弹出提示框
      this.$message.success('修改成功')
      this.isShowPermission = false
    },
    // 取消
    btnPermCancel() {
      // 清空所选复选框的值
      this.checkList = []
      this.isShowPermission = false
    },
    // 权限设置相关
    async   showPermission(id) {
      // 数据回显多选框
      const { permIds } = await getRoleDetail(id)
      this.checkList = permIds
      this.roleId = id
      this.isShowPermission = true
    }

  }

}
</script>

  <style>

  </style>
